﻿using System;
using System.IO;
using System.Windows;
using System.Configuration;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Application = System.Windows.Application;
using MessageBox = System.Windows.MessageBox;

namespace AccountManagement
{
    /// <summary>
    /// Interaction logic for FrmCompany.xaml
    /// </summary>
    public partial class FrmCompany : Window
    {
        //private readonly LinqHelper<AccountsDataContext> _helper = new LinqHelper<AccountsDataContext>();

        public FrmCompany()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Company Infomation
                var oConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                oConfig.AppSettings.Settings["TenCongTy"].Value = txtCompanyName.Text;
                oConfig.AppSettings.Settings["DiaChi"].Value = txtAddress.Text;
                oConfig.AppSettings.Settings["Tel"].Value = txtTel.Text;
                oConfig.AppSettings.Settings["Fax"].Value = txtFax.Text;
                oConfig.AppSettings.Settings["MST"].Value = txtMST.Text;
                oConfig.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");

                //// Account
                //var objAcc = _helper.GetOne<tblAccount>(t => t.SoTK == 111);
                //if (objAcc == null)
                //{
                //    MessageBox.Show("Không thể cập nhật tài khoản tiền mặt.", "Lỗi", MessageBoxButton.OK,
                //                MessageBoxImage.Warning);
                //    return;
                //}

                //objAcc.SoDu = Convert.ToDecimal(txtTKTM.Text.Trim());

                //_helper.Update(objAcc,t=>t.SoTK == objAcc.SoTK);

                MessageBox.Show("Lưu thông tin công ty thành công.", "Thông báo", MessageBoxButton.OK,
                                MessageBoxImage.Information);
                Close();
            }
            catch (Exception)
            {

            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                txtCompanyName.Text = ConfigurationManager.AppSettings.Get("TenCongTy");
                txtAddress.Text = ConfigurationManager.AppSettings.Get("DiaChi");
                txtTel.Text = ConfigurationManager.AppSettings.Get("Tel");
                txtFax.Text = ConfigurationManager.AppSettings.Get("Fax");
                txtMST.Text = ConfigurationManager.AppSettings.Get("MST");

                //var objAcc = _helper.GetOne<tblAccount>(t => t.SoTK == 111);
                //if (objAcc != null)
                //    txtTKTM.Text = objAcc.SoDu.ToString();
                //else
                //{
                //    MessageBox.Show("Không đọc được thông tin tài khoản tiền mặt.", "Lỗi", MessageBoxButton.OK,
                //                MessageBoxImage.Warning);
                //}
            }
            catch (Exception)
            {
                
            }
        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var fileDialog = new OpenFileDialog();
                fileDialog.ShowDialog();
                if (string.IsNullOrEmpty(fileDialog.FileName))
                    return;

                var path = System.Reflection.Assembly.GetExecutingAssembly().Location;
                if (path != null)
                {
                    path = path.Remove(path.LastIndexOf("\\"));
                    if (!Directory.Exists(path + "\\Logos"))
                    {
                        Directory.CreateDirectory(path + "\\Logos");
                    }
                    File.Copy(fileDialog.FileName,
                              path + "\\Logos" +
                              fileDialog.FileName.Substring(fileDialog.FileName.LastIndexOf("\\")));

                    var src = new Uri(@"/AccountManagement;component/Logos/" + fileDialog.FileName.Substring(fileDialog.FileName.LastIndexOf("\\") + 1), UriKind.Relative);
                    var img = new BitmapImage(src);
                    imgLogo.Source = img;
                    //ImageSource imageSource = new BitmapImage(new Uri("Logos/" + ,UriKind.Relative));
                    //imgLogo.Source = 
                }
            }
            catch (Exception)
            {
                
            }
        }
    }
}
